from django.shortcuts import render
from rest_framework.viewsets import ReadOnlyModelViewSet
from rest_framework_extensions.cache.mixins import CacheResponseMixin
from .serializers import AreaSerializer
from .serializers import SubAreaSerializer
from .models import Area


class AreaViewSet(CacheResponseMixin, ReadOnlyModelViewSet):
    """行政区划视图集"""
    # 关闭分页功能
    pagination_class = None

    # 重写当前视图获取序列化类的方法，根据动作实例化不同的序列化器对象
    def get_serializer_class(self, *args, **kwargs):
        # 判断当前请求动作[action]
        if self.action == 'list':
            return AreaSerializer
        else:
            return SubAreaSerializer

    def get_queryset(self):
        """
        提供数据集
        """
        # 根据不同的动作，把顶级的行政区划分离出来查询
        if self.action == 'list':
            return Area.objects.filter(parent=None)
        else:
            return Area.objects.all()

    # def list(self, request, *args, **kwargs):
    #     """查询省份列表"""
    #
    # def retrieve(self, request, *args, **kwargs):
    #
